package net.mihome.app.ui.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.util.Log;
import com.loopj.android.http.RequestParams;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.adapter.ListViewBaoxiuAdapter;
import net.mihome.app.adapter.ListViewDianpuAdapter;
import net.mihome.app.bean.Article;
import net.mihome.app.bean.RepairRequire;
import net.mihome.app.bean.Shop;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.UIHelper;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;

import org.apache.http.Header;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.maxwin.view.XListView;

public class MifuwuBaoxiuListView extends XListView {
	private Handler handler;
	private ListViewBaoxiuAdapter adapter;
	private List<RepairRequire> listData = new ArrayList<RepairRequire>();
	private AppContext appContext;
	private int begin=0;
	private int num=10;
	private int newsTypeId=Article.ARTICLE_TYPE_BANZHENG;
	private MyViewLinster myViewLinster;
	public String titleSearchString=null;
	public MifuwuBaoxiuListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread arg0, Throwable arg1) {
				// TODO Auto-generated method stub
				Log.d("hello", arg0.getName());
				Log.d("hello2", arg1.getLocalizedMessage());
				arg1.printStackTrace();
			}
		});
		afterInit();
		// TODO Auto-generated constructor stub
	}
	public MifuwuBaoxiuListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		afterInit();
		// TODO Auto-generated constructor stub
	}
	private void afterInit(){
		adapter = new ListViewBaoxiuAdapter(getContext(),R.layout.active_listitem, listData);
		setAdapter(adapter);
        setPullLoadEnable(true);
        setPullRefreshEnable(true);
		appContext=(AppContext) getContext().getApplicationContext();
		setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
                if(myViewLinster!=null){
                    myViewLinster.onBeforeLoad();
                }
                begin=0;
                num=10;
                RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
                requestParams.add("begin",begin+"");
                requestParams.add("num",num+"");
                appContext.ASYNC_HTTP_CLIENT.get(URLs.MIHOME_GET_REPAIR_LIST, requestParams, new MiAsyncHttpResponseHandler<List<Map>>() {
                    @Override
                    public void onSuccess(int arg0, Header[] arg1, ResponsePojo<List<Map>> responsePojo) {
                        List<RepairRequire> list = RepairRequire.parserList(responsePojo);
                        if (list != null && !list.isEmpty()) {
                            listData.clear();
                            listData.addAll(list);
                            adapter.notifyDataSetChanged();
                            if (myViewLinster != null) {
                                myViewLinster.onLoadComplete();
                            }
                        }
                        stopRefresh();
                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                        if (myViewLinster != null) {
                            myViewLinster.onLoadComplete();
                        }
                        Toast.makeText(appContext, arg3.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d("mihome","occur exception ",arg3);
                        stopRefresh();
                    }
                });
			}

			@Override
			public void onLoadMore() {
				begin=(begin+1)*num;
                num=10;
                RequestParams requestParams=new RequestParams();
                requestParams.add("begin",begin+"");
                requestParams.add("num",num+"");
                appContext.ASYNC_HTTP_CLIENT.get(URLs.MIHOME_GET_REPAIR_LIST, requestParams, new MiAsyncHttpResponseHandler<List<Map>>() {
                    @Override
                    public void onSuccess(int arg0, Header[] arg1, ResponsePojo<List<Map>> responsePojo) {
                        List<RepairRequire> list = RepairRequire.parserList(responsePojo);
                        if (list != null && !list.isEmpty()) {
                            listData.addAll(list);
                            adapter.notifyDataSetChanged();
                            if (myViewLinster != null) {
                                myViewLinster.onLoadComplete();
                            }
                        }
                        if(myViewLinster!=null){
                            myViewLinster.onLoadComplete();
                        }
                        stopLoadMore();
                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                        if (myViewLinster != null) {
                            myViewLinster.onLoadComplete();
                        }
                        Toast.makeText(appContext, arg3.getMessage(), Toast.LENGTH_SHORT).show();
                        stopLoadMore();
                    }
                });
			}
		});

        setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                RepairRequire repairRequire =listData.get(position);
                UIHelper.showBaoxiuDetail(view.getContext(), Long.valueOf(id).intValue());
            }
        });
	}
	public void initData(){
		if(myViewLinster!=null){
			myViewLinster.onBeforeLoad();
		}
        startRefresh();
	}
	public MyViewLinster getMyViewLinster() {
		return myViewLinster;
	}
	public void setMyViewLinster(MyViewLinster myViewLinster) {
		this.myViewLinster = myViewLinster;
	}

}
